package service

import (
	"fmt"
	"hanyun-admin/src/entity"
	"log"
)

//根据userId查询出角色
func SelectRolesByUserId(userId int64) (role entity.Role) {
	_, err := DbEngine.Table("sys_role").Alias("r").
		Join("LEFT", []string{"sys_user_role", "ur"}, "r.role_id=ur.role_id").
		Join("LEFT", []string{"sys_user", "u"}, "u.user_id=ur.user_id").
		Join("LEFT", []string{"sys_dept", "d"}, "u.dept_id=d.dept_id").
		Where("r.del_flag=?", "0").And("u.user_id=?", userId).Get(&role)
	if err != nil {
		fmt.Println("select role error", err)
	}
	return
}
func GetRolePermission(user entity.User) (arr []string) {
	arr = make([]string, 0)
	if user.IsAdmin() {
		arr = append(arr, "admin")
	} else {

	}
	return
}

//获取所有角色
func SelectRoleAll() []entity.Role {
	role := make([]entity.Role, 0)
	err := DbEngine.Table("sys_role").Alias("r").
		Join("LEFT", []string{"sys_user_role", "ur"}, "r.role_id=ur.role_id").
		Join("LEFT", []string{"sys_user", "u"}, "u.user_id=ur.user_id").
		Join("LEFT", []string{"sys_dept", "d"}, "u.dept_id=d.dept_id").Find(&role)
	if err != nil {
		log.Println("select all role by error ", err)
	}
	return role
}

func SelectRoleListByUserId(userId int64) []int64 {
	role := make([]entity.Role, 0)
	err := DbEngine.Alias("r").Join("LEFT", []string{"sys_user_role", "ur"}, "ur.role_id = r.role_id").
		Join("LEFT", []string{"sys_user", "u"}, "u.user_id=ur.user_id").Where("u.user_id=?", userId).Find(&role)
	if err != nil {
		log.Println("select role list by userId error ", err)
	}
	roleIdList := make([]int64, 0)
	for _, v := range role {
		roleIdList = append(roleIdList, v.RoleId)
	}
	return roleIdList
}
